package com.winz.cultureapp.fragment;

import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.View;
import android.view.ViewGroup;

import com.mine.xrecyclerview.XRecyclerView;
import com.mine.xrecyclerview.rcvadapter.OnItemClickListener;
import com.winz.cultureapp.R;
import com.winz.cultureapp.activity.AdminStadiumDesActivity;
import com.winz.cultureapp.adapter.xrcv.OrderRecordAdapter;
import com.winz.cultureapp.base.BaseFragment;
import com.winz.cultureapp.constants.Constants;
import com.winz.cultureapp.entity.RecordListBean;
import com.winz.cultureapp.http.OnResponeListener;
import com.winz.cultureapp.http.request.RequestFactory;
import com.winz.cultureapp.http.util.HttpUitls;
import com.winz.cultureapp.utils.JsonUtil;
import com.winz.cultureapp.utils.Logger;
import com.winz.cultureapp.utils.SharePrefsUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 管理员 -- 场馆认证记录
 */
public class AdminStadiumFragment extends BaseFragment implements XRecyclerView.LoadingListener, OnItemClickListener {
    @BindView(R.id.xrcv_record)
    XRecyclerView mXrcv_list;

    private boolean isRefresh = true;
    private int limit = 10;
    private int page = 1;
    private int totalPage = 0;

    private List<RecordListBean.DataBean.ListBean> mDatas;
    private OrderRecordAdapter mAdapter;

    public static AdminStadiumFragment getInstance(){
        AdminStadiumFragment fragment = new AdminStadiumFragment();

        return fragment;
    }

    @Override
    protected int contentView() {
        return R.layout.fragment_admin_stadium;
    }

    @Override
    protected void initViews(View view) {
        mDatas = new ArrayList<>();
        // XrecyclerView
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mXrcv_list.setLayoutManager(manager);
        mXrcv_list.setPullRefreshEnabled(true);
        mXrcv_list.setLoadingMoreEnabled(false);
        mAdapter = new OrderRecordAdapter(mContext,mDatas,R.layout.empty_view,R.layout.item_order_record);
    }

    @Override
    protected void initEvents() {

        mXrcv_list.setAdapter(mAdapter);
        mXrcv_list.setLoadingListener(this);
        mAdapter.setOnItemClickListener(this);


    }

    @Override
    protected void loadDatas() {
        getList();
    }


    /**
     * 获取数据列表
     */
    private void getList() {
        showProgressDialog();
        HttpUitls.onGetByToken("admin_user_unit_book_list", SharePrefsUtil.getInstance().getString(Constants.USER_INFO.TOKEN), RequestFactory.getInstance().unit_book_fix_record(page+"",limit+""),new OnResponeListener<RecordListBean>() {
            @Override
            public void onSuccess(RecordListBean bean) {
                Logger.d("admin_user_unit_book_list==>>", JsonUtil.getJsonFromObj(bean));

                HttpUitls.isExpire(bean.code);

                if (bean.code == 0){
                    totalPage = bean.totalPage;
                    if (bean.data.list != null && bean.data.list.size()>0){
                        mXrcv_list.setLoadingMoreEnabled(true);
                        if (isRefresh){
                            mDatas.clear();
                            mDatas = bean.data.list;
                        }else {
                            mDatas.addAll(bean.data.list);
                        }
                    }else {
                        mDatas.clear();
                        RecordListBean.DataBean.ListBean empty = new RecordListBean.DataBean.ListBean();
                        empty.layout_type = -1;
                        mDatas.add(empty);
                        mXrcv_list.setLoadingMoreEnabled(false);
                    }
                    mAdapter.setDatas(mDatas);
                }else {
                    showMsg(bean.msg);
                }
                if (isRefresh){
                    mXrcv_list.refreshComplete();
                }else {
                    mXrcv_list.loadMoreComplete();
                }
                dismissProgressDialog();
            }

            @Override
            public void onFailed(String error) {
                dismissProgressDialog();
                Logger.d("admin_user_unit_book_list==>>", error);
                if (isRefresh){
                    mXrcv_list.refreshComplete();
                }else {
                    mXrcv_list.loadMoreComplete();
                }
            }
        });
    }


    /**
     * 刷新
     */
    @Override
    public void onRefresh() {
        isRefresh = true;
        page = 1;
        getList();
    }

    /**
     * 加载
     */
    @Override
    public void onLoadMore() {
        isRefresh = false;
        if (page>=totalPage){
            mXrcv_list.setNoMore(true,true,"没有更多数据了");
        }else {
            page++;
            getList();
        }
    }


    @Override
    public void onItemClick(ViewGroup parent, View view, int position) {
        Bundle bundle = new Bundle();
        bundle.putSerializable(Constants.KEY.ADMIN.DETAIL,mDatas.get(position));
        startActivity(AdminStadiumDesActivity.class,false,bundle);
    }

    @Override
    public boolean onItemLongClick(ViewGroup parent, View view, int position) {
        return false;
    }
}
